package com.android.webrtcsdkdev.net

import okhttp3.Interceptor
import okhttp3.Response

/**

 * @Author : wy

 * @Time : On 2024/10/8 17:26

 * @Description : RequestInterceptor

 */
class RequestInterceptor : Interceptor {
    private val TAG = "AAAAA"

    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val requestBuilder = request.newBuilder()
        val httpUrl = request.url().newBuilder().build()
        // 请求头map
      //  var headerMap = HashMap<String, String>()
//        when (request.method()) {
//            "GET" -> {
//                // 解析url传递参数
//                val paramKeys = httpUrl.queryParameterNames()
//                // TreeMap 默认元素迭代升序
//                val map = TreeMap<String, String>()
//                for (key: String in paramKeys) {
//                    map[key] = httpUrl.queryParameter(key).toString()
//                }
//                // 制作带签名的请求头map
//                headerMap = RequestHeader.makeRequestHeaderMap(map, httpUrl)
//                LogUtil.i(TAG, "GET headerMap $headerMap")
//            }
//            "POST" -> {
//                // TreeMap 默认元素迭代升序
//                val map = TreeMap<String, String>()
//                // 解析body传递参数
//                when (val body = request.body()) {
//                    is FormBody -> {
//                        for (i in 0 until body.size()) {
//                            map[body.encodedName(i)] = body.encodedValue(i)
//                        }
//                    }
//                    is MultipartBody -> {
//                    }
//                    else -> {
//                        val buffer = Buffer()
//                        body?.writeTo(buffer)
//
//                        var charset = Charset.forName("UTF-8")
//                        val contentType = body?.contentType()
//                        if (contentType != null) {
//                            charset = contentType.charset(charset)
//                        }
//                        if (StringUtil.isPlaintext(buffer)) {
//                            val content = buffer.readString(charset)
//                            val jsonObject = JSONObject(content)
//                            for (i in jsonObject.keys()) {
//                                map[i] = jsonObject.get(i).toString()
//                            }
//                        }
//                    }
//                }
//                // 制作带签名的请求头map
//                headerMap = RequestHeader.makeRequestHeaderMap(map, httpUrl)
//                Log.i(TAG, "POST headerMap $headerMap")
//            }
//        }

        // 添加请求头map 到请求头
//        for (item in headerMap) {
//            requestBuilder.addHeader(item.key, item.value)
//        }
        requestBuilder.addHeader("ak", "8e70c3cb-6509-452d-ba98-93f25b79b38d")
        requestBuilder.addHeader("version", "1")
        requestBuilder.addHeader("sign", "123456")
        return chain.proceed(requestBuilder.build())
    }
}
